const axios = require('axios')
const config = require('../../config')
const { client_id, client_secret, request_token_url } = config.github

const generateToken = async function(code) {
  const result = await axios({
    method: 'POST',
    url: request_token_url,
    data: {
      client_id,
      client_secret,
      code
    },
    headers: {
      Accept: 'application/json; charset=utf-8'
    }
  })

  return result
}

const getUserInfo = async function (url, Authorization) {
  const userInfo = await axios({
    method: 'GET',
    url: url,
    headers: {
      Authorization
    }
  })

  return userInfo
}

module.exports = {
  generateToken,
  getUserInfo
}